<?php 
    require("../../wp-blog-header.php");
	require('../../wp-config.php');
	$e_conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
		or die("Can't connect: " . mysqli_error($e_conn));
	$PATH = $_SERVER['PHP_SELF'];

	//function get user role
	function get_user_role()
	{
		global $wpdb;
		$uid = wp_get_current_user()->ID;
		$role = $wpdb->get_var("SELECT meta_value FROM {$wpdb->usermeta} WHERE meta_key = 'wp_capabilities' AND user_id = {$uid}");
		  if(!$role) return 'non-user';
		$rarr = unserialize($role);
		$roles = is_array($rarr) ? array_keys($rarr) : array('non-user');
		return $roles[0];
	}

	//check user role
	if(get_user_role() != "administrator")
    {
        die("You are not Admin");
    }

	//Get list group
	$query = "SELECT `group_id`, `group_name` FROM `phpbb_groups` ORDER BY `group_id`";
	$result = mysqli_query($e_conn, $query);
	$group_data = array();
	$i = 0;
	while ($row = mysqli_fetch_array($result)) {
		$group_data[$i] = $row;
		$i++;
	}

	//Get infomation
	$GROUP_ID = -1;
	$LIST = "";
	if (isset($_POST['group_id']) && isset($_POST['list'])) {
		$GROUP_ID = $_POST['group_id'];
		$LIST = $_POST['list'];
	}
?>
<html>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<head>
	<title>Users List Creator</title>
	<style type="text/css">
		body{
			width: 1000px;
			margin: auto;
		}
		label {
			width: 60px;
			display: inline-block;
		}
		form{
			width: 500px;
		}
		.left{
			float: left;
		}
		.right{
			float: right;
			width: 500px;
		}
		.red{
			color: red;
			font-size: 2em;
			float: right;
		}
	</style>
</head>
<body>
	<div class="left">
	<form method="POST" action="<?php echo $PATH?>">
			<p>Nhập danh sách MSSV, mỗi MSSV trên 1 dòng:</p>
			<textarea cols="30" rows="15" name="list"><?php echo $LIST; ?></textarea><br />
			<label>Nhóm:</label>
			<select name="group_id">
			<?php 
				foreach ($group_data as $key => $group) {
					$gId = $group['group_id'];
					$gName = $group['group_name'];
					if ($gId == $GROUP_ID || ($GROUP_ID == -1 && $gId == 3)) {
						echo "<option value='$gId' selected='selected'>($gId) $gName</option>";
					}
					else {
						echo "<option value='$gId'>($gId) $gName</option>";
					}
					
				}
			 ?>
			</select>
			<br />
			<input type="submit" name="submit" value="Submit">
	</form>
	<p>Lưu ý:</p>
	<ul>
		<li>REGISTERED: Nhóm chỉ dùng được trong diễn đàn</li>
		<li>REGISTERED-COPPA: Nhóm dùng được trong cả 2</li>
	</ul>
	</div>
	
<?php 
	if (isset($_POST['group_id']) && isset($_POST['submit'])) {
		$result = "";

		if ($_POST['submit'] != null) {
			//Get information to textbox
			$list = explode("\n", $LIST);
			$i = 1;
			foreach ($list as $key => $row) {
				$id = $row;
				if(count($list) != $i){
					$id = substr($row, 0, -1);
				}
				$email = $id . "@hcmut.edu.vn";
				$pwd = $id;
				$line = '"' . $id . '","' . $pwd . '","' . $email . '",' . $GROUP_ID . "\n";
				$result .= $line;
				$i++;
			}
			if ($GROUP_ID == '' || $LIST == '') {
				echo "<p class=\"red\">Cảnh báo: Chưa có đủ thông tin</p>";
			}
			else {
				echo "<div class='right'>
						<form method=\"POST\" action='import_users.php'>
							<p>\"Username\",\"password\",\"email\",group</p>
							<textarea cols='60' rows='15' name='list'>$result</textarea><br />
							<input type=\"submit\" name=\"submit\" value='Thêm các user này'>
						</form>
					</div>";
				echo "<p>Kiểm tra trước khi thêm user</p>";
			}
		}	
	}
?>
</body>
</html>
